package com.example.demo.service;

import com.example.demo.utils.DDPP;
import com.example.demo.bean.PageRetrieveData;
import com.example.demo.bean.SimpleRetrieveData;
import com.example.demo.dao.RetrieveDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RetrieveServiceImpl implements RetrieveService{
    @Autowired
    private RetrieveDao rd;
    //简单查询只根据flag进行select查询,flag表示接口名称
    public List<Map<String,Object>> simpleRetreive(Map<String,String[]> params){
        List<Map<String,Object>> resultMap=new ArrayList<>();
        Class c=rd.getClass();
        Map<String,Object> sqlParams=new HashMap<>();
        //提取data
        String interfaceName="";
        for(String key:params.keySet()){
            if(key.equalsIgnoreCase("flag")){
                //提取flag获取接口名
                interfaceName=params.get("flag")[0];
            }
            else{
                //获取sql参数
                sqlParams.put(key,params.get(key)[0]);
            }
        }
        Method[] methods=c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if(interfaceName.equals(methods[i].getName())){
                try {
                    resultMap= (List<Map<String, Object>>) methods[i].invoke(rd,sqlParams);
                    return resultMap;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("没有找到"+interfaceName+"的接口");
        return resultMap;
    }
    public Map<String,Object> multiRetrieve(Map<String,String[]> params){
        Map<String,Object> result= new HashMap<>();
        List<Map<String,String[]>> paramMaps=DDPP.ddpp(params);
        for(Map<String,String[]> params1:paramMaps){
            if(params1.containsKey("pageSize")&&params1.containsKey("pageNum")){
                PageRetrieveData pageRetrieveData=new PageRetrieveData();
                List<Map<String,Object>> resultMap=pagingRetreive(params1);
                String flag=params1.get("flag")[0];
                int pageSize=Integer.parseInt(params1.get("pageSize")[0]);
                int pageNum=Integer.parseInt(params1.get("pageNum")[0]);
                int total=resultMap.size();
                int pageTotal=(int)Math.ceil(total*1.0/pageSize);
                int i=(pageNum-1)*pageSize;
                int j=i+pageSize;
                List<Map<String,Object>> resultMap2=new ArrayList<>();
                for (;i<j;i++){
                    resultMap2.add(resultMap.get(i));
                }
                pageRetrieveData.setCode(200);
                pageRetrieveData.setData(resultMap2);
                pageRetrieveData.setPageTotal(pageTotal);
                pageRetrieveData.setTotal(total);
                pageRetrieveData.setMessage("查询成功");
                result.put(flag,pageRetrieveData);
            }
            else{
                String flag=params1.get("flag")[0];
                SimpleRetrieveData simpleRetrieveData=new SimpleRetrieveData();
                List<Map<String,Object>> resultMap=simpleRetreive(params1);
                simpleRetrieveData.setCode(200);
                simpleRetrieveData.setData(resultMap);
                simpleRetrieveData.setMessage("查询成功");
                result.put(flag,simpleRetrieveData);
            }
        }
        return result;
    }
    public List<Map<String,Object>> pagingRetreive(Map<String,String[]> params){
        List<Map<String,Object>> resultMap=new ArrayList<>();
        Class c=rd.getClass();
        String interfaceName="";
        Map<String,Object> sqlParams=new HashMap<>();
        for(String key:params.keySet()){
            if(key.equalsIgnoreCase("flag")){
                //提取flag获取接口名
                interfaceName=params.get("flag")[0];
            }
            else{
                //获取sql参数
                sqlParams.put(key,params.get(key)[0]);
            }
        }
        Method[] methods=c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if(interfaceName.equals(methods[i].getName())){
                try {
                    resultMap= (List<Map<String, Object>>) methods[i].invoke(rd,sqlParams);
                    return resultMap;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("没有找到"+interfaceName+"的接口");
        return resultMap;
    }

}
